package jmcnet.libcommun.logger
{
	import jmcnet.libcommun.communs.helpers.HelperDate;
	import jmcnet.libcommun.communs.time.Time;
	import jmcnet.libcommun.communs.newjmcnetds.BlazeDSService;
	
	import mx.rpc.remoting.RemoteObject;
	
	public class JMCNetLogger
	{
		private static var level:int = LEVEL_INFO;
		private static var _log_enabled:Boolean = true;
		private static var _remoteLog_enabled:Boolean = true;
		private static var _remoteObject:BlazeDSService = null;
		
		protected static const LEVEL_DEBUG:int = 10;
		protected static const LEVEL_INFO:int = 20;
		protected static const LEVEL_EVT:int = 25;
		protected static const LEVEL_WARN:int = 30;
		protected static const LEVEL_ERROR:int = 40;
		protected static const LEVEL_FATAL:int = 50;
		
		public static const DEBUG:String = "DEBUG";
		public static const INFO:String = "INFO";
		public static const EVT:String = "EVT";
		public static const WARN:String = "WARNING";
		public static const ERROR:String = "ERROR";
		public static const FATAL:String = "FATAL";
		
		public static const LOCALLOG:String = "LOCALLOG";
		public static const REMOTELOG:String = "REMOTELOG";
		public static const NOLOG:String = "NOLOG";
		
		public static function getRemoteLogger():BlazeDSService {
			return _remoteObject;
		}
		
		public static function setRemoteLogger(service:BlazeDSService):void {
			_remoteObject = service;
		}
		
		public static function get remoteLogEnabled():Boolean { return _remoteLog_enabled; }
		public static function get logEnabled():Boolean { return _log_enabled; }
		
		public static function debug(message:String):void{
				log(LEVEL_DEBUG, message);
		}
		
		public static function info(message:String):void{
				log(LEVEL_INFO, message);
		}
		
		public static function evt(message:String):void{
				log(LEVEL_EVT, message);
		}
		
		public static function warn(message:String):void{
				log(LEVEL_WARN, message);
		}
		
		public static function error(message:String):void{
				log(LEVEL_ERROR, message);
		}
		
		
		
		public static function log(levelMessage:int, message:String):void{
			
			if(! _log_enabled) return;
			
			var d:Date=new Date();
			var t:Time=new Time();
			t.setCurrentTime();
			
			var ds:String = new HelperDate(HelperDate.XML_DATE_FORMAT).dateToString(d)+" "+t.timeToString();
						
			switch(levelMessage){
				case LEVEL_DEBUG:
					if(level <= LEVEL_DEBUG){
//						Logger.log(message,LoggerLevel.DEBUG);
						trace("[DEBUG] : "+ds+" : "+message);
					}
				break;
				case LEVEL_INFO:
					if(level <= LEVEL_INFO){
//						Logger.log(message,LoggerLevel.INFO);
						trace("[INFO] : "+ds+" : "+message);
					}
				break;
				case LEVEL_EVT:
					if(level <= LEVEL_EVT){
//						Logger.log(message,LoggerLevel.INFO);
						trace("[EVT] : "+ds+" : "+message);
					}
				break;
				case LEVEL_WARN:
					if(level <= LEVEL_WARN){
//						Logger.log(message,LoggerLevel.WARN);
						trace("[WARNING] : "+ds+" : "+message);
					}
				break;
				case LEVEL_ERROR:
					if(level <= LEVEL_ERROR){
//						Logger.log(message,LoggerLevel.ERROR);
						trace("[ERROR] : "+ds+" : "+message);
					}
				break;
			}
		}
		
		public static function initJMCNetLogger(logLevel:String, logMode:String, serviceLogger:BlazeDSService=null):void {
			trace("LogLevel="+logLevel+" logMode="+logMode+" serviceLogger="+serviceLogger);
			
			if (logLevel != null) logLevel=logLevel.toUpperCase();
			setLogLevel(logLevel);
			
			if (logMode != null) logMode=logMode.toUpperCase();
			
			switch (logMode) {
				case LOCALLOG :
					JMCNetLogger.setLogEnabled(true, false);
					break;
				case REMOTELOG :
					JMCNetLogger.setLogEnabled(true, true);
					break;
				case NOLOG :
					JMCNetLogger.setLogEnabled(false, false);
				default :
					trace("Attention : logMode="+logMode+" inconnu. Valeurs possibles : "+LOCALLOG+" "+REMOTELOG+" "+NOLOG);
			}
			
			if (serviceLogger != null) {
				JMCNetLogger.setRemoteLogger(serviceLogger);
			}
			trace("Les logs sont configurés");
		}
		
		public static function setLogLevel(logLevel:String):void{

			if (logLevel == DEBUG) {
				level = LEVEL_DEBUG;
			} else if(logLevel == INFO){
				level = LEVEL_INFO;
			} else if(logLevel == WARN){
				level = LEVEL_WARN;
			} else if(logLevel == ERROR){
				level = LEVEL_ERROR;
			} else if(logLevel == FATAL) {
				level = LEVEL_FATAL;
			} else {
				trace("Attention : logLevel : '"+logLevel+"' inconnu. Niveau de log non positionné");
			}
		}
		
		public static function setLogEnabled(enabled:Boolean, remoteLogEnabled:Boolean=false):void{
				_log_enabled = enabled;
				_remoteLog_enabled = remoteLogEnabled;
		}
	}
}